package cz.swosh.stomp.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

/**
 * Odhlašuje
 * @author swosh
 */
public class Logout extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    private static final String url = "/stompear-web/index.jsp?page=logout";

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#HttpServlet()
     */
    public Logout() {
        super();
    }

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request, response);
    }

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/xhtml+xml;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");

        // Najde přihlášeného
        Subject subject = SecurityUtils.getSubject();

        if (subject != null) {
            //see:  http://jsecurity.org/api/index.html?org/jsecurity/web/DefaultWebSecurityManager.html
            subject.logout();
        }

        HttpSession session = request.getSession(false);
        if (session != null) {
            session.invalidate();
        }


        response.sendRedirect(url);

    }

}
